<?php

class Menu_item extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    function _remap($method, $params = array()) {
        $this->_admin_remap($method, $params);
    }

    function index($menu_id) {
        $data = array();
        $data['menu_id'] = $menu_id;
        //--------------------
        $this->load->model('Menu_model');
        $row = $this->Menu_model->get_by_field(array('id' => $menu_id), 'title', true);
        $data['menu_title'] = $row['title'];
        //---------------------
        $this->load->model('Menu_item_model');
        $row = $this->Menu_item_model->get_by_field(array('menu_id' => $menu_id), 'id', true);
        $data['root_id'] = $row['id'];
        $this->_setLayout('admin/menu_item/menu_item_list', 'admin_layout', $data);
    }

    function add() {
        $callback = $_REQUEST['callback'];
        $jsonOutPut = '';
        try {
            $data = json_decode($_GET['data'], true);
            if (empty($data)) {
                $jsonOutPut = json_encode(array('success' => false, 'message' => 'Can not save'));
            } else {
                $this->load->model('Menu_item_model');
                if (isset($data['id'])) {
                    $this->Menu_item_model->update($data, $data['id']);
                    $data_return = $this->Menu_item_model->get_by_id($data['id']);
                    $data_return['edit'] = '<a href="' . base_url() . 'admin/menu_item/edit/' . $data['id'] . '"><img src="' . base_url() . 'public/skin/back-end/images/edit.png"></a>';
                    $jsonOutPut = json_encode(array('success' => true, 'message' => 'Saved successfully', 'data' => $data_return));
                } else {
                    $id = $this->Menu_item_model->insert($data);
                    $data_return = $this->Menu_item_model->get_by_id($id);
                    $data_return['edit'] = '<a href="' . base_url() . 'admin/menu_item/edit/' . $id . '"><img src="' . base_url() . 'public/skin/back-end/images/edit.png"></a>';
                    $jsonOutPut = json_encode(array('success' => true, 'message' => 'Loaded data', 'data' => $data_return));
                }
            }
        } catch (Exception $e) {
            $jsonOutPut = json_encode(array('success' => false, 'message' => 'Can not save'));
        }
        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . $jsonOutPut . ');';
        } else {
            header('Content-Type: application/x-json');
            echo $jsonOutPut;
        }
    }

    function delete() {
        $callback = $_REQUEST['callback'];
        $jsonOutPut = '';
        try {
            $data = json_decode($_GET['data'], true);
            $this->load->model('Menu_item_model');
            $this->Menu_item_model->delete_all_child($data);
            $jsonOutPut = json_encode(array('success' => true));
        } catch (Exception $e) {
            $jsonOutPut = json_encode(array('success' => false));
        }
        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . $jsonOutPut . ');';
        } else {
            header('Content-Type: application/x-json');
            echo $jsonOutPut;
        }
    }

    function grid() {
        $this->load->model('Menu_item_model');
        $callback = $_REQUEST['callback'];
        $data = array();
        foreach ($this->Menu_item_model->getAllTree($_GET['menu_id'], "---", true, 2) as $row) {
            $data[] = array_merge($row, array('edit' => '<a href="' . base_url() . 'admin/menu_item/edit/' . $row['id'] . '/' . $_GET['menu_id'] . '"><img src="' . base_url() . 'public/skin/back-end/images/edit.png"></a>'));
        }
        $jsonOutPut = json_encode(array('success' => true, 'message' => 'Loaded data', 'data' => $data));
        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . $jsonOutPut . ');';
        } else {
            header('Content-Type: application/x-json');
            echo $jsonOutPut;
        }
    }

    function get_nodes() {
        $this->load->model('Menu_item_model');
        $data = array();
        $nodes = array();
        $listItem = $this->Menu_item_model->getChild($_POST['node'], $_GET['menu_id']);
        if (count($listItem) > 0)
            foreach ($listItem as $row) {
                $nodes[] = array(
                    'text' => $row['title'],
                    'id' => $row['id'],
                    'expanded' => true
                );
            }
        echo json_encode($nodes);
    }

    function edit($id=0, $menu_id) {
        $this->load->model('Menu_item_model');
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $input_data = $_POST;
            if (!empty($id) && $this->Menu_item_model->updatemenu($input_data, $id)) {
                die(json_encode(array('success' => true)));
            } elseif (empty($id) && ($id = $this->Menu_item_model->insert($input_data, $id))) {
                die(json_encode(array('success' => true)));
            } else {
                die(json_encode(array('success' => false)));
            }
        }
        $this->load->library('helpfunc');
        $currentNode = (empty($id)) ? array() : $this->Menu_item_model->get_by_id($id);
        $prexNode = (empty($id)) ? array() : $this->Menu_item_model->getNodebyRight($currentNode['lft'] - 1, $menu_id);
        $parentNode = (empty($id)) ? array() : $this->Menu_item_model->getParent($id);
        $nextNode = (empty($id)) ? array() : $this->Menu_item_model->getNodebyLeft($currentNode['rgt'] + 1, $menu_id);
        $allchildarr = (empty($id)) ? array() : $this->Menu_item_model->getallchild($id, $menu_id);
        $result = array();
        foreach ($allchildarr as $item)
            array_push($result, $item['id']);
        $allchildarr = $result;

        $data = array('datacontent' => $currentNode,
            'preNode' => $prexNode,
            'nextNode' => $nextNode,
            'parentNode' => $parentNode,
            'allchildarr' => $allchildarr
        );


        $data['menu_id'] = $menu_id;
        $menulist = $this->Menu_item_model->get_by_field(array('menu_id' => $menu_id));
        foreach ($menulist as $item) {
            $parent_option[$item['id']] = $item['title'];
        }
        $data['parent_id'] = 0;
        $data['parent_option'] = $parent_option;
        $data['alltree'] = $this->Menu_item_model->getAllTree($menu_id);
        $this->_setLayout('admin/menu_item/menu_item_edit', 'admin_layout', $data);
    }

    //ajax
    function getchild() {
        if (!isset($_GET['current_id']))
            $_GET['current_id'] = null;
        if (!isset($_GET['prenode_id']))
            $_GET['prenode_id'] = -1;
        if ($_GET['prenode_id'] == -1 && isset($_GET['nextnode_id']))
            $select = "selected = 'selected'";
        else
            $select = "";
        $this->load->model('Menu_item_model');
        $data = $this->Menu_item_model->getchild($_GET['id'], $_GET['menu_id'], $_GET['current_id']);
        if (count($data) == 0)
            die;
        $str = "<th scope=\"row\">Position</th>
            <td align=\"left\">
                <select name='position'>                    
                    <option  value='after'>after</option>
                    <option {$select} value='before'>before</option>
                </select> - <select name='position_item'>";
        foreach ($data as $item) {
            if ($_GET['prenode_id'] == $item['id'])
                $str .= "<option selected='selected' value='{$item['id']}'>{$item['title']}</option>";
            else
                $str .= "<option value='{$item['id']}'>{$item['title']}</option>";
        }
        $str .= "</select></td>";
        echo $str;
    }

    public function move_nodes() {
        $current_node = $this->input->post('current_node');
        $ref_node = $this->input->post('ref_node');
        $point = $this->input->post('point');
        $menu_id = $this->input->post('menu_id');

        $this->load->model('Menu_item_model');

        try {
            if ($point == 'append') {
                $this->Menu_item_model->move($current_node, $ref_node, $menu_id);
            } else {
                $position = $point == 'below' ? 2 : 1;
                $parentRefNode = $this->Menu_item_model->getParent($ref_node);
                $this->Menu_item_model->move($current_node, $parentRefNode['id'], $menu_id);
                $this->Menu_item_model->order($current_node, $ref_node, $menu_id, $position);
            }

            echo json_encode(array('success' => true));
        } catch (Exception $e) {
            echo json_encode(array('success' => false));
        }
    }

}

?>
